package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.service.inter.UserService;
import com.yunfeisoft.business.model.NurseSchedule;
import com.yunfeisoft.business.service.inter.NurseScheduleService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: NurseScheduleController
 * Description: 护士排班表Controller
 * Author: Jackie liu
 * Date: 2020-05-22
 */
@Controller
public class NurseScheduleController extends BaseController {

    @Autowired
    private NurseScheduleService nurseScheduleService;

    @Autowired
    private UserService userService;

    /**
     * 添加护士排班表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nurseSchedule/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(NurseSchedule record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "userId", "护士为空");
        validator.required(request, "scheduleDate", "排班日期为空");
        validator.required(request, "floor", "楼层为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        nurseScheduleService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改护士排班表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nurseSchedule/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(NurseSchedule record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "userId", "护士为空");
        validator.required(request, "scheduleDate", "排班日期为空");
        validator.required(request, "floor", "楼层为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        nurseScheduleService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询护士排班表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nurseSchedule/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        NurseSchedule record = nurseScheduleService.load(id);
        if (!ObjectUtils.isEmpty(record)) {
            User user = userService.load(record.getUserId());
            record.setUsername(user.getName());
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询护士排班表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nurseSchedule/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        Page<NurseSchedule> page = nurseScheduleService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 根据时间区间查询护士排班记录
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nurseSchedule/listByTime", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response listByTime(HttpServletRequest request, HttpServletResponse response) {
        String start = ServletRequestUtils.getStringParameter(request, "start", null);
        String end = ServletRequestUtils.getStringParameter(request, "end", null);
        if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
            return ResponseUtils.warn("请选择开始结束时间");
        }
        Timestamp startTime = Timestamp.valueOf(start);
        Timestamp endTime = Timestamp.valueOf(end);
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("start", startTime);
        params.put("end", endTime);
        List<NurseSchedule> list = nurseScheduleService.listByTime(params);
        return ResponseUtils.success(list);
    }

    /**
     * 批量删除护士排班表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nurseSchedule/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        nurseScheduleService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
